package spitter.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import domain.Spitter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class SpitterRepository {

    @Autowired
    private DataSource dataSource;

    /**
     * 根据用户名查找
     * @param userName
     * @return
     */
    public Spitter findByUserName(String userName){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        Spitter spitter = null;
        try {
            //查询数据
            connection = dataSource.getConnection();
            String sql = "select u.* from t_spitter u where u.username = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userName);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String username1 = resultSet.getString(2);
                String password = resultSet.getString(3);
                Boolean enabled = resultSet.getBoolean(4);
                spitter = new Spitter(username1,password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if(!connection.isClosed()){
                    connection.close();
                }
                if(!preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return spitter;
    }

    /**
     * 插入一条用户数据
     * @param spitter
     * @return
     */
    public Integer save(Spitter spitter){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int modified=0;
        try {
            //插入数据
            connection = dataSource.getConnection();
            String sql = "insert into t_spitter(username,password,enabled) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, spitter.getUserName());
            preparedStatement.setString(2, spitter.getPassword());
            preparedStatement.setBoolean(3, spitter.getEnabled());
            modified = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if(!connection.isClosed()){
                    connection.close();
                }
                if(!preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return modified;
    }

}
